﻿/*
 * Created by SharpDevelop.
 * User: Zoila
 * Date: 23/10/2011
 * Time: 04:50 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Business_Entity;
using System.Collections.Generic;

namespace PetSoft.GUI
{
	/// <summary>
	/// Description of Utils.
	/// </summary>
	/// 
	public static class Utils
	{
		public static BEUsuario  usuario ;
		
		public static BEUsuario  empleado ;
		
		public static String rutaFotoNoDisponible = "..//mascota_no_disponible.jpg";
		
		public static Boolean esClienteMaster(BECliente cliente){
			if(cliente.Nombre.Equals("PETSOFT") && cliente.ApellidoPaterno.Equals("PETSOFT") && cliente.ApellidoMaterno.Equals("PETSOFT"))
				return true;
			else
			 	return false;
		}
		
		public static void SoloLetrasTB(object sender, KeyPressEventArgs e)
		{
			
			 if(!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)&& (e.KeyChar!=' ')) 
			 {
			
				e.Handled=true; 
			  }
			 else{
				e.Handled=false;
			}
			
		}
		
		public static void NoCaracteresRarosTB(object sender, KeyPressEventArgs e)
		{
			if((e.KeyChar=='%') || (e.KeyChar=='"') || (e.KeyChar=='\'')  )
			 {
				e.Handled=true; 
			 }
			 else{
				e.Handled=false;
			}
			
		}
		
		public static void ForzarMayusculasTB(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            // make it uppercase only
            if (c >= 'a' && c <= 'z')
            {
                int digit = (int)c;
                digit = digit - 'a' + 'A';
                e.KeyChar = Convert.ToChar(digit);
            }
        }
		
		public static void SoloNumerosTB(object sender, KeyPressEventArgs e)
		{
			if(!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
			{
				e.Handled = true;
			}
			else
			{
				e.Handled = false;
			}
				
		}
		
		public static void DecimalesTB(object sender, KeyPressEventArgs e)
		{
			//int cont=0;// contador de decimales
			int val = 0, cont = 0;
			bool IsDec = false;
			//int cont=0;// contador de decimales
			if(!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar) && (e.KeyChar!='.'))
			{
				//if (e.KeyChar=='.'){ cont++;}
				e.Handled = true;
			}
			if(char.IsNumber(e.KeyChar)){
				for (int i=0 ; i<(sender as TextBox).Text.Length; i++) {
			         if ( (sender as TextBox).Text[i] == '.' )
			            IsDec = true;
			
			         if ( IsDec && cont++ >=2) {
			            e.Handled = true;
			            return;
			         }		
			     }
				if(!IsDec){
					e.Handled = false;
				}
			}
			if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
		    {
				e.Handled = true;
				val = 1;
			}	
		}
		public static void DecimalesPuntoTB(object sender, KeyPressEventArgs e)
		{
			//int cont=0;// contador de decimales
			int val = 0, cont = 0;
			bool IsDec = false;
			//int cont=0;// contador de decimales
			if(!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar) && (e.KeyChar!='.'))
			{
				//if (e.KeyChar=='.'){ cont++;}
				e.Handled = true;
			}
			if(char.IsNumber(e.KeyChar)){
				for (int i=0 ; i<(sender as TextBox).Text.Length; i++) {
			         if ( (sender as TextBox).Text[i] == '.' )
			            IsDec = true;
			
			         if ( IsDec && cont++ >=2) {
			            e.Handled = true;
			            return;
			         }		
			     }
				if(!IsDec){
					e.Handled = false;
				}
			}
			if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
		    {
				e.Handled = true;
				val = 1;
			}
				
		}
		public static void CambiaDefaultCB (object sender, EventArgs e)
		{
			ComboBox combo= (ComboBox) sender;
			if (combo.SelectedIndex!=-1)
				combo.ForeColor=System.Drawing.Color.Black;
			else 
				combo.ForeColor=System.Drawing.Color.Turquoise;
			
		}
		
		public static void llenaComboSexo(System.Windows.Forms.ComboBox cb){			
			cb.Items.Add("<SELECCIONAR>");
			cb.Items.Add("MASCULINO");
			cb.Items.Add("FEMENINO");
			cb.SelectedIndex = 0;	
		}
		
		
		
		
		
		public static void ParaUsuarioTB(object sender, KeyPressEventArgs e)
        {
			if(e.KeyChar==' '){
				e.Handled = false;
			}
			else{
	
				if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && (e.KeyChar!='.'))
	            {
	                e.Handled = true;
	            }
	            else
	            {
	                e.Handled = false;
	            }
			}

        }
		
		public static void SoloLetrasNumerosTB(object sender, KeyPressEventArgs e)
        {
			if((e.KeyChar==' ') || (e.KeyChar=='º') || (e.KeyChar=='.')){
				e.Handled = false;
			}
			else{
	
	            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
	            {
	                e.Handled = true;
	            }
	            else
	            {
	                e.Handled = false;
	            }
			}

        }	
		
		public static List<int> getCheckedIndexes(DataGridView dgv, String checkColumnName, String indexColumnName){
			List<int> lista = new List<int>();
			
			foreach(DataGridViewRow row in dgv.Rows){
				DataGridViewCell cell = row.Cells[checkColumnName];
				if (cell.Value != null && (bool)cell.Value){
				lista.Add((int)row.Cells[indexColumnName].Value);
				}
			}
			
			return lista;
		}

		public static void dateMenorQueNow(object sender, EventArgs e){
			DateTime now = DateTime.Now;
			if (((DateTimePicker)sender).Value > now){
				((DateTimePicker)sender).Value = now;
			}
		}
		
		public static void dateMayorQueNow(object sender, EventArgs e){
			DateTime now = DateTime.Now;
			if (((DateTimePicker)sender).Value < now){
				((DateTimePicker)sender).Value = now;
			}
		}
		
		public static int getSelectedIndex(DataGridView dgv){
			if (dgv.SelectedRows == null || dgv.SelectedRows.Count == 0) return -1;
			
			DataGridViewRow row = dgv.SelectedRows[0];
			return dgv.Rows.IndexOf(row);
		}
		
		
		public static void checkDobleClick(DataGridView dgv, String checkColumn){
			int i = getSelectedIndex(dgv);
			
			if (i != -1){
				if (dgv.Rows[i].Cells[checkColumn].Value == null){
					dgv.Rows[i].Cells[checkColumn].Value = false;
				}
				dgv.Rows[i].Cells[checkColumn].Value = !((bool)dgv.Rows[i].Cells[checkColumn].Value);
			}
		}

		public static void seleccionarTodo(DataGridView dgv, object checkbox, String checkColumn){
			bool valor = ((CheckBox)checkbox).Checked;
			foreach(DataGridViewRow row in dgv.Rows){
				row.Cells[checkColumn].Value = valor;
			}
		}
		
		public static void dateMenorQue(DateTimePicker desde, DateTimePicker hasta){
			if (desde.Value > hasta.Value){
				desde.Value = hasta.Value;
			}
		}
		
		public static void dateMayorQue(DateTimePicker hasta, DateTimePicker desde){
			if (desde.Value > hasta.Value){
				hasta.Value = desde.Value;
			}
		}
		
		public static void MessageError(String mensaje, Exception ex = null){
			String errorStr = "";
			if (ex != null) errorStr = "\n"+ex.ToString();
			MessageBox.Show(mensaje + errorStr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		
		public static void MessageAviso(String mensaje, Exception ex = null){
			String errorStr = "";
			if (ex != null) errorStr = "\n"+ex.ToString();
			MessageBox.Show(mensaje + errorStr, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
		}
		
		public static bool MessagePregunta(String mensaje){
			DialogResult res = MessageBox.Show(mensaje, "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question);			
			if(res == DialogResult.Yes) return true;
			else return false;
		}
		
		public static void MessageInformation(String mensaje){
			MessageBox.Show(mensaje, "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
		
		public static String getRutaEncabezadoReporte(){
			String rutaHeader = "..\\..\\Iconos\\REPORTE_HEADER.jpg";
			return rutaHeader;
		}
		
		public static iTextSharp.text.Image getEncabezadoReporte(){
			iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Utils.getRutaEncabezadoReporte());
			img.ScaleToFit(500, 55);
			return img;
		}
		
	}
}
